<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        .layui-btn-warm {
            background-color: #FFB800;
            color: #fff;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!-- 搜索表单 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">部门名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon"></i> 搜 索
                            </button>
                            <button type="button" class="layui-btn layui-btn-normal layui-btn-sm" id="resetSearchBtn">
                                <i class="layui-icon">ဂ</i> 重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!-- 工具栏 -->
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="add">添加部门</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">批量删除</button>
            </div>
        </script>

        <!-- 表格容器 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 表格操作按钮模板 -->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>
        </script>

        <!-- 添加/编辑部门弹窗 -->
        <div id="deptEdit" style="display: none; padding: 20px;">
            <form class="layui-form" lay-filter="deptForm">
                <input type="hidden" name="depId">
                <div class="layui-form-item">
                    <label class="layui-form-label">部门名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="depName" required lay-verify="required" placeholder="请输入部门名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="depCode" required lay-verify="required" placeholder="请输入部门编码" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">部门描述</label>
                    <div class="layui-input-block">
                        <textarea name="depMark" placeholder="请输入部门描述" class="layui-textarea"></textarea>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="saveDept">保存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        // 初始化表格
        table.render({
            elem: '#currentTableId',
            url: '/department/queryPage',
            method: 'post',
            contentType: 'application/json',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'depId', width: 80, title: 'ID', sort: true},
                {field: 'depName', width: 150, title: '部门名称'},
                {field: 'depCode', width: 120, title: '部门编码'},
                {field: 'depMark', title: '部门描述'},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]],
            limits: [10, 20, 50],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function(res) {
                return {
                    "code": res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.list
                };
            },
            response: {
                statusName: 'code',
                statusCode: 0,
                msgName: 'msg',
                countName: 'count',
                dataName: 'data'
            }
        });

        // 搜索功能
        form.on('submit(data-search-btn)', function (data) {
            table.reload('currentTableId', {
                page: { curr: 1 },
                where: {
                    name: data.field.name
                }
            });
            return false;
        });
        //重置
        $('#resetSearchBtn').on('click', function () {
            table.reload('currentTableId', {
                page: { curr: 1 },
                where: {
                    name: ''
                }
            });
        });

        // 工具栏事件监听
        table.on('toolbar(currentTableFilter)', function (obj) {
            switch (obj.event) {
                case 'add': // 添加部门
                    form.val('deptForm', {
                        depId: '',
                        depName: '',
                        depCode: '',
                        depMark: ''
                    });
                    layer.open({
                        title: '添加部门',
                        type: 1,
                        area: ['500px', '350px'],
                        content: $('#deptEdit')
                    });
                    break;

                case 'delete': // 批量删除
                    var checkStatus = table.checkStatus('currentTableId');
                    if (checkStatus.data.length === 0) {
                        layer.msg('请选择要删除的部门');
                        return;
                    }
                    var depIds = checkStatus.data.map(item => item.depId);
                    layer.confirm('确定删除选中的部门吗？', function () {
                        // 循环删除每个选中的部门
                        var deleteCount = 0;
                        var totalCount = depIds.length;

                        $.ajax({
                            url: '/department/deleteIds',
                            type: 'POST',
                            contentType: 'application/json',
                            data: JSON.stringify(depIds),
                            success: function(res) {
                                if (res.code === 0) {
                                    layer.msg('删除成功');
                                    table.reload('currentTableId');
                                } else {
                                    layer.msg('删除失败：' + res.msg);
                                }
                            }
                        })
                    });
                    break;
            }
        });

        // 行工具事件监听
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                form.val('deptForm', {
                    depId: data.depId,
                    depName: data.depName,
                    depCode: data.depCode,
                    depMark: data.depMark
                });
                layer.open({
                    title: '编辑部门',
                    type: 1,
                    area: ['500px', '350px'],
                    content: $('#deptEdit')
                });
            } else if (obj.event === 'delete') {
                layer.confirm('确定删除该部门吗？', function () {
                    $.ajax({
                        url: '/department/delete/' + data.depId,
                        type: 'GET',
                        success: function(res) {
                            if (res.code === 0) {
                                layer.msg('删除成功');
                                obj.del();
                            } else {
                                layer.msg('删除失败: ' + res.msg);
                            }
                        }
                    });
                });
            }
        });

        // 保存部门
        form.on('submit(saveDept)', function (data) {
            var method = data.field.depId ? 'update' : 'add';
            $.ajax({
                url: '/department/' + method,
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data.field),
                success: function(res) {
                    if (res.code === 0) {
                        layer.msg('保存成功');
                        layer.closeAll();
                        table.reload('currentTableId');
                    } else {
                        layer.msg('保存失败: ' + res.msg);
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>
